package com.wqiuqiu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wqiuqiu.common.annotation.ListValue;
import com.wqiuqiu.common.constant.ProductConstant;
import com.wqiuqiu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wqiuqiu.gulimall.product.dao.AttrDao;
import com.wqiuqiu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wqiuqiu.gulimall.product.entity.AttrEntity;
import com.wqiuqiu.gulimall.product.service.AttrAttrgroupRelationService;
import com.wqiuqiu.gulimall.product.service.AttrService;
import com.wqiuqiu.gulimall.product.vo.AttrResVo;
import com.wqiuqiu.gulimall.product.vo.RelGroupAndAttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.Query;

import com.wqiuqiu.gulimall.product.dao.AttrGroupDao;
import com.wqiuqiu.gulimall.product.entity.AttrGroupEntity;
import com.wqiuqiu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    AttrDao attrDao;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (StringUtils.isNotEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId != 0) {
            wrapper.and((obj) -> {
                obj.eq("catelog_id", catelogId);
            });
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> findAttrList(Long attrgroupId) {

        QueryWrapper<AttrAttrgroupRelationEntity> attr_attr_group_id = new QueryWrapper<AttrAttrgroupRelationEntity>().eq(
                "attr_group_id", attrgroupId);
        List<AttrAttrgroupRelationEntity> attrattrGroupEntities = attrAttrgroupRelationDao.selectList(attr_attr_group_id);
        if (attrattrGroupEntities.size() != 0) {
            List<Long> attrIds = attrattrGroupEntities.stream().map((obj) -> obj.getAttrId()).collect(Collectors.toList());
            List<AttrEntity> attrEntities = attrDao.selectBatchIds(attrIds);
            return attrEntities;
        }
        return null;
    }

    @Override
    public PageUtils queryNoRelAttrPage(Map<String, Object> params, Long attrgroupId) {
        //找出分类下的其他组未关联的属性，同时去除该属性组已经关联的属性，这里的属性是基本属性也就是attrtype=1
        //找出该属性组的属性分类
        AttrGroupEntity attrGroupEntity = this.baseMapper.selectById(attrgroupId);
        //找出属性分类的全部属性组
        List<AttrGroupEntity> groupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq(
                "catelog_id", attrGroupEntity.getCatelogId()
        ));
        List<Long> attrGroupIds = groupEntities.stream().map((obj) -> {
            return obj.getAttrGroupId();
        }).collect(Collectors.toList());
        //找出属性组已经关联的全部属性
        List<Long> attrIds = attrAttrgroupRelationDao.findGroupAttr(attrGroupIds);
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq(
                "attr_type", ProductConstant.enumAttr.BASE_ATTR.getCode()
        ).eq("catelog_id", attrGroupEntity.getCatelogId());

        if (attrIds.size() != 0) {
            wrapper.and((obj) -> {
                obj.notIn("attr_id", attrIds);
            });
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("attr_id", key).or().like("attr_name", key);
        }
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    @Override
    public List<RelGroupAndAttrVo> queryRelGroupAndAttr(Long catelogId) {

        //根据分类id得到该分类绑定的全部分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq(
                "catelog_id", catelogId
        ));
        //根据分组id找到分组绑定的全部属性id
        List<RelGroupAndAttrVo> collect = attrGroupEntities.stream().map(obj -> {
            RelGroupAndAttrVo relGroupAndAttrVo = new RelGroupAndAttrVo();
            BeanUtils.copyProperties(obj, relGroupAndAttrVo);
            //根据属性id找到全部属性信息
            List<AttrEntity> attrList = this.findAttrList(obj.getAttrGroupId());
            relGroupAndAttrVo.setAttrs(attrList);
            return relGroupAndAttrVo;
        }).collect(Collectors.toList());

        return collect;

    }


}